package dnl.dtools.grm.operations;

/**
 * 
 * @author daniel
 * 
 */
public abstract class BaseResourceOperation {

	private int progress;

	private ProgressListener progressListener;

	public ProgressListener getProgressListener() {
		return progressListener;
	}

	public void setProgressListener(ProgressListener progressListener) {
		this.progressListener = progressListener;
	}

	public abstract void operate() throws ResourceOperationException;

	public int getProgress() {
		return progress;
	}

	protected final void setProgress(int progress) {
		this.progress = progress;
		if(getProgressListener() != null){
			getProgressListener().progressed(progress);
		}
	}

	protected final void finished() {
		getProgressListener().finished();
	}
	
	protected final void setProgress(double d) {
		if (d < 0 || d > 1)
			throw new IllegalArgumentException("d must be a fraction.");
		setProgress((int) (d * 100));
	}
}
